package com.echat.messagesender.sender.config;

import com.echat.messagesender.sender.exception.EchatSmsException;
import com.echat.messagesender.sender.mapper.PlatformAccountMapper;
import com.echat.messagesender.sender.mapper.PlatformMapper;
import com.echat.messagesender.sender.mapper.SmsProfileMapper;
import com.echat.messagesender.sender.model.Platform;
import com.echat.messagesender.sender.model.PlatformAccount;
import com.echat.messagesender.sender.sms.SmsSender;
import com.echat.messagesender.sender.sms.SmsSenderManager;
import com.echat.messagesender.sender.sms.bean.AliSmsConfig;
import com.echat.messagesender.sender.sms.bean.Echat253Config;
import com.echat.messagesender.sender.sms.bean.SmsConfig;
import com.echat.messagesender.sender.sms.bean.SmsWayConfig;
import com.echat.messagesender.sender.sms.impl.AliSmsSender;
import com.echat.messagesender.sender.sms.impl.Echat253Sender;
import com.echat.messagesender.sender.sms.impl.SmsWaySender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by chenmohan on 2018/09/06
 *
 * @description
 */
@Configuration
public class SmsSenderConfig {

    @Autowired
    SmsProfileMapper smsProfileMapper;

    @Autowired
    PlatformMapper platformMapper;

    @Bean
    public SmsSenderManager smsSenderManager() {

        List<SmsSender> smsSenderList = smsProfileMapper.selectAll().parallelStream()
                .collect(Collectors.groupingBy(
                        e -> e.getPlatformAccountId(),
                        Collectors.toMap(e -> e.getKey(), e -> e.getValue()
                        )))
                .entrySet().stream().map(e -> {
                    Platform platform = platformMapper.selectByPlatformAccountId(e.getKey());
                    if (platform == null) {
                        throw new EchatSmsException("platformAccountId = " + e.getKey() + " 的平台账号不存在!");
                    }
                    Long platformId = platform.getId();
                    SmsConfig smsConfig = new SmsConfig(platformId, platform.getPlatformName() , e.getKey(), e.getValue());
                    return instanceEmailSender(smsConfig);
                }).collect(Collectors.toList());

        SmsSenderManager smsSenderManager = new SmsSenderManager();
        smsSenderManager.setSmsSenderList(smsSenderList);
        Map<Long, SmsSender> smsSenderMap = smsSenderList.parallelStream().collect(Collectors.toMap(e -> e.getPlatformAccountId(), e -> e));
        smsSenderManager.setSmsSenderMap(smsSenderMap);
        return smsSenderManager;
    }

    /**
     * 根据不同的短信配置来创建不同的消息发送Sender
     *
     * @param config
     * @return
     */
    private SmsSender instanceEmailSender(SmsConfig config) {
        switch (config.getServerType()) {
            case AliCloud:
                return new AliSmsSender((AliSmsConfig) config.get());
            case SmsWay:
                return new SmsWaySender((SmsWayConfig) config.get());
            case Echat253:
                return new Echat253Sender((Echat253Config) config.get());
        }
        return null;
    }
}
